package org.brisling.common.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.util.Map;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

/**
 * 
 * <p>Title: FileUtil</p>
 * <p>Description: 文件操作工具类</p>
 * <p>Company: tongking</p>
 * @author jackson wang
 * @since 1.0
 * @version 1.0
 * @date 2017年3月24日 下午3:36:25
 */
public class FileUtil {

	private static FileUtil fu;
	
	private static final String file_encoding = "UTF-8";
	
	private FileUtil(){};
	
	/**
	 * 获取实例
	 * @return
	 */
	public static FileUtil getInstance(){
		if(fu==null)
			fu = new FileUtil();
		return fu;
	}
	
	/**
	 * 获取文件扩展名
	 * @param fileName	文件全名
	 * @return
	 */
	public static String getExpandName(String fileName){
		
		String expandName = null;
		if(fileName!=null){
			String[] names = fileName.split("\\.");
			if(names!=null && names.length>0){
				expandName = names[names.length-1];
			}
		}		
		return expandName;		
	}
	
	
	//freemarker 模板配置
    private static Configuration cfg = new Configuration(Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS);
    
	/**
	 * 导出word工具类方法
	 * @param param		传入参数
	 * @param TemplatePathName	文档模板
	 * @param outfile			导出文件名称
	 */
	public static void exportWord(Map<String,Object> param,String TemplatePathName,String outfile){
		
    	Template template = null;        
       
        try {
        	
        	FileReader fr = new FileReader(TemplatePathName);
			cfg.setDefaultEncoding("UTF-8");
			
        	template = new Template("",fr,cfg);

			File outFile = new File(outfile);
			
			Writer out =  new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile),"UTF-8"));
			
			template.process(param, out); 
			out.flush();
			out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (TemplateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}        
    	
	}
	
	/**
	 * 读取文件内容	
	 * @param filePathName	文件路径及名称
	 * @param encoding		文件编码
	 * @return
	 */
	public static String readFile(String filePathName,String encoding){
		
		String ret = null;
		if(filePathName!=null){
			//获取文件编码类型
			String _coding = encoding==null?file_encoding:encoding;
			//定义文件输入流
			InputStreamReader isr = null;
			FileInputStream fis = null;
			StringBuffer buffer = new StringBuffer();
			try {
				//创建文件输入流和流读取对象
				fis = new FileInputStream(filePathName);
				isr = new InputStreamReader(fis,_coding);
				
							
				//读取文件
				int c = 0;
				while((c=isr.read())!=-1){
					buffer.append((char)c);
				}
				
				
				//关闭流读取对象和文件输入流对象
				isr.close();
				fis.close();
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			if(buffer!=null)
				ret = buffer.toString();
		}
		
		return ret;
	}
	
	/**
	 * 导出word工具类方法
	 * @param param		传入参数
	 * @param TemplatePathName	文档模板
	 * @param OutputStream		输出数据流
	 */
	public static void exportWord(Map<String,Object> param,String TemplatePathName,OutputStream os){
		
    	Template template = null;
               
        try {
        	
        	
        	       	
        	InputStreamReader isr=new InputStreamReader(new FileInputStream(TemplatePathName),"UTF-8");
        	BufferedReader read = new BufferedReader(isr);
        	
        	cfg.setDefaultEncoding("UTF-8");
			
        	template = new Template("",read,cfg);
//        	template = cfg.getTemplate(TemplateName);
//			template = new Template("",inSql,cfg);		
			
			Writer out =  new BufferedWriter(new OutputStreamWriter(os,"UTF-8"));		
			
			template.process(param, out); 
			out.flush();
			out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (TemplateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}        
    	
	}
	
	public static void outFile(String file){
		File outFile = new File(file);
		
		Writer out;
		try {
			out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile),"UTF-8"));
			out.flush();
			out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
	}
}
